//
//  UIImageView+shotScreen.swift
//  SYWeibo
//
//  Created by Saya on 16/4/15.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
extension UIImage{
    class func shotScreenWithView(view:UIView) -> UIImage{
        UIGraphicsBeginImageContext(view.frame.size)
        let context:CGContextRef = UIGraphicsGetCurrentContext()!
        
        view.layer.renderInContext(context)
        let tempImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return tempImage
    }
    
    //给当前屏幕截图
    class func snapshotScreen() -> UIImage {
        let window = UIApplication.sharedApplication().keyWindow!
        //开启图片上下文
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, UIScreen.mainScreen().scale)
        //将window显示的内容画到上下文中
        window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: false)
        
        //从上下文中 获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return image
    }
}
